﻿using ERP.ProductionManage;
using ERP.QualityAssurance;
using ERP.QualityAssurance.Dto;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Volo.Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using ERP.ERPModels.QualityAssurance;

namespace ERP.Web.Controllers.QualityAssurance
{
    public class TestitemsettingsController : Controller
    {
        private readonly ITestitemsettingService _testitemsettingsService;
        private readonly IRepository<Testitemsettings, int> _testitemsettingsRepository;
        private readonly Volo.Abp.ObjectMapping.IObjectMapper _objectMapper;

        public TestitemsettingsController(
            ITestitemsettingService testitemsettingsService,
            IRepository<Testitemsettings, int> testitemsettingsRepository,
            Volo.Abp.ObjectMapping.IObjectMapper objectMapper)
        {
            _testitemsettingsService = testitemsettingsService;
            _testitemsettingsRepository = testitemsettingsRepository;
            _objectMapper = objectMapper;
        }

        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取检测项分页列表
        /// </summary>
        /// <param name="dto">查询条件</param>
        [HttpPost]
        public async Task<JsonResult> GetList([FromBody] TestitemsettingListDto dto)
        {
            // 创建查询
            var query = await _testitemsettingsRepository.GetQueryableAsync();

            // 应用筛选条件
            if (!string.IsNullOrWhiteSpace(dto.ItemSettingsCode))
            {
                query = query.Where(x => x.ItemSettingsCode.Contains(dto.ItemSettingsCode));
            }

            if (!string.IsNullOrWhiteSpace(dto.ItemSettingsName))
            {
                query = query.Where(x => x.ItemSettingsName.Contains(dto.ItemSettingsName));
            }

            if (!string.IsNullOrWhiteSpace(dto.ItemSettingsType))
            {
                query = query.Where(x => x.ItemSettingsType == dto.ItemSettingsType);
            }

            // 获取总记录数
            var totalCount = await query.CountAsync();

            // 应用排序
            query = query.OrderBy(x => x.Id);

            // 应用分页
            int pageSize = dto.PageSize > 0 ? dto.PageSize : 10;
            int pageIndex = dto.PageIndex > 0 ? dto.PageIndex : 1;
            var list = await query.Skip((pageIndex - 1) * pageSize)
                                .Take(pageSize)
                                .ToListAsync();

            // 返回结果
            return Json(new
            {
                code = 0,
                msg = "success",
                count = totalCount,
                data = list
            });
        }

        [HttpPost]
        public async Task<IActionResult> Create([FromBody] CreateTestitemsettingDto input)
        {
            var result = await _testitemsettingsService.CreateAsync(input);
            return Json(new { success = true, data = result });
        }

        [HttpPost]
        public async Task<IActionResult> Update(int id, [FromBody] UpdateTestitemsettingDto input)
        {
            var result = await _testitemsettingsService.UpdateAsync(id, input);
            return Json(new { success = true, data = result });
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            await _testitemsettingsService.DeleteAsync(id);
            return Json(new { success = true });
        }

        [HttpPost]
        public async Task<IActionResult> BatchDelete([FromBody] List<int> ids)
        {
            await _testitemsettingsService.BatchDeleteAsync(ids);
            return Json(new { success = true });
        }

        [HttpGet]
        public async Task<IActionResult> Export()
        {
            var fileBytes = await _testitemsettingsService.ExportAsync();
            return File(fileBytes, "text/csv", "Testitemsettings.csv");
        }
    }
}
